﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using BusinessLogicLayer;

namespace logicunivesityinventorystore.Store
{
    public partial class PurchaseStationary : System.Web.UI.Page
    {
        List<OrderDetails> orderDet = new List<OrderDetails>();
        protected void Page_Load(object sender, EventArgs e)
        {
           orderDet=(List<OrderDetails>)Session["OrderDetails"];
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            var query = from order in orderDet
                        select new
                        {
                            ItemCode = order.ItemCode,
                            Quantity = order.QtyOrdered,
                            Description = order.Remark,
                        };
            gvItemList.DataSource = query;
            gvItemList.DataBind();
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            string clerkID = (string)Session["UserName"];
            DateTime expectedDate = Convert.ToDateTime(txtExDate.Text);
            orderDet = new List<OrderDetails>();
            foreach (GridViewRow row in gvItemList.Rows)
            {
                OrderDetails order = new OrderDetails();
                string supplierID=(row.FindControl("ddlSupplier")as DropDownList).SelectedItem.ToString();
                string orderID = new OrderFacade().GetOrderID(supplierID,clerkID,expectedDate);
                int qtyOrdered=Convert.ToInt32((row.FindControl("txtQty") as TextBox).Text);
                string itemCode=row.Cells[0].Text;
                order.PONo = orderID;
                order.QtyOrdered = qtyOrdered;
                order.ItemCode=itemCode;
                Boolean flag=new OrderFacade().ValidateExist(orderID,itemCode);
                if (!flag)
                {
                    orderDet.Add(order);
                }
                else
                {
                    new OrderFacade().UpdateOrderQty(orderID, itemCode, qtyOrdered);
                }
                new ItemFacade().UpdateLogicStock(itemCode, qtyOrdered);
            }
            new OrderFacade().insertOrderDetails(orderDet);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string itemCode = e.Row.Cells[0].Text;
                int qty = Convert.ToInt32((e.Row.FindControl("txtQty") as TextBox).Text);
                DropDownList ddl = (e.Row.FindControl("ddlSupplier") as DropDownList);
                List<string> supplier = new ItemFacade().GetSuppliers(itemCode);
                foreach(string supp in supplier)
                ddl.Items.Add(supp);
                string supplierID = (e.Row.FindControl("ddlSupplier") as DropDownList).SelectedItem.ToString();
                string ContactNumber = new SuppliersFacade().FindContactByID(supplierID);
                e.Row.Cells[4].Text = ContactNumber;
                float price = (float)new SuppliersFacade().FindPriceByItemCodeAndSupplierID(itemCode,supplierID);
                e.Row.Cells[5].Text = price.ToString();
                if (qty > 0)
                { 
                    e.Row.Cells[6].Text = (price * qty).ToString(); 
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            GridViewRow row = (GridViewRow)ddl.NamingContainer;
            if (row != null)
            {
                string supplierID = ddl.SelectedValue.ToString();
                row.Cells[4].Text = new SuppliersFacade().FindContactByID(supplierID);
            }
        }
        
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                    int index = Convert.ToInt32( e.CommandArgument.ToString());
                    orderDet.RemoveAt(index);
                    Session["OrderDetails"] = orderDet;
                    BindGrid();
                
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ArrayList description = (ArrayList)Session["SelectedItems"];

            foreach (string desc in description)
            {
                OrderDetails ordDet = new OrderDetails();
                Item item = new Item();
                ordDet.ItemCode = desc;
                ordDet.QtyOrdered = 0;
                item = new ItemFacade().FindItemByID(desc);
                ordDet.Remark = item.Description;
                orderDet.Add(ordDet);
            }
            BindGrid();
        }

        protected void tbx_qty_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            GridViewRow row = (GridViewRow)tb.NamingContainer;
            if (row != null)
            {
                int qty=Convert.ToInt32((row.FindControl("txtQty") as TextBox).Text);
                float price=(float)Convert.ToDouble(row.Cells[5].Text);
                if (qty > 0)
                {
                    row.Cells[6].Text = (qty * price).ToString();
                }
            }
        }
    }
}